package com.financing.platform.web.api;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FpVideoDTO;
import com.financing.platform.service.IFpVideoService;
import com.financing.platform.util.DTOdateSet;

@Controller("fpVideoController")
@RequestMapping("/api/fpVideo")
public class FpVideoController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpVideoService fpVideoService;
	
	@RequestMapping("/listFpVideo.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpVideoDTO fpVideo,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpVideo list begin .......");
			
			fpVideo.setBegin(pager.getStartIndex());
			fpVideo.setPageSize(pager.getPageSize());
			
			//过滤删除的视频
			fpVideo.setDeleteTag("1");
			int count = fpVideoService.queryListCount(fpVideo);
			
			pager.setTotalCount(count);
			
			List<FpVideoDTO> fpVideoList = null;
			
			if(count > 0){
				fpVideoList = fpVideoService.queryList(fpVideo);
			}else{
				fpVideoList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpVideoList);
			result.setStatus(STATUS.SUCCESS);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			logger.info("query fpVideo list end .......");
		}catch(Exception e){
			log.error("query fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpVideo.shtml")
	@ResponseBody
	public synchronized ResultMap add(HttpServletRequest request, FpVideoDTO fpVideo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpVideo  begin .......");
			
			fpVideo.setDeleteTag("1");
			
			if("1".equals(fpVideo.getIsEncryption())){
				fpVideo.setEncryptionPassword("SFA");
			}
			
			DTOdateSet.setCreateInfo(fpVideo,request.getSession());
			DTOdateSet.setUpdateInfo(fpVideo,request.getSession());
			
			int id = fpVideoService.insert(fpVideo);
			result.setAttribute("id", id);
			
			result.setStatus(STATUS.SUCCESS);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			logger.info("add fpVideo list end .......");
		}catch(Exception e){
			log.error("add fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpVideo.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpVideoDTO fpVideo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpVideo  begin .......");
			
			Integer id = fpVideo.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpVideoDTO dto = fpVideoService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpVideo  end .......");
		}catch(Exception e){
			log.error("query detail fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpVideo.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpVideoDTO fpVideo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpVideo  begin .......");
			
			Integer id = fpVideo.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			fpVideoService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpVideo  end .......");
		}catch(Exception e){
			log.error("delete fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpVideo.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpVideoDTO fpVideo,Integer oldId){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpVideo  begin .......");
			
			
			Map<String, Object>  map = getParameterMap(request);	
			DTOdateSet.setUpdateInfo(fpVideo,request.getSession());
			map.put("updateBy", fpVideo.getUpdateBy());
			map.put("updateDate", fpVideo.getUpdateDate());
			
			Integer id = fpVideo.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			//老视频ID ，产品解绑老视频
			if(oldId != null){
				fpVideoService.updateFpVideoForProduct(oldId);
			}
			
			fpVideoService.update(map);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpVideo list end .......");
		}catch(Exception e){
			log.error("update fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	
	@RequestMapping("/updateForceFpVideo.shtml")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpVideoDTO fpVideo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpVideo  begin .......");
			
			Integer id = fpVideo.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(fpVideo,request.getSession());
			
			fpVideoService.updateForce(fpVideo);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpVideo list end .......");
		}catch(Exception e){
			log.error("updateForce fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpVideoPassword.shtml")
	@ResponseBody
	public ResultMap updateFpVideoPassword(HttpServletRequest request, FpVideoDTO fpVideo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpVideo  begin .......");
			
			
			if(StringUtils.isBlank(fpVideo.getEncryptionPassword())){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			
			fpVideoService.updateFpVideoPassword(fpVideo);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpVideo list end .......");
		}catch(Exception e){
			log.error("updateForce fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/queryVideoInfo.shtml")
	@ResponseBody
	public ResultMap queryVideoInfo(HttpServletRequest request, FpVideoDTO fpVideo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query Video Info  begin .......");
			
			
			FpVideoDTO dto = fpVideoService.queryVideoInfo(fpVideo);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query Video Info  end .......");
		}catch(Exception e){
			log.error("query Video Info error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	/**
	 * 从request中获得参数Map，并返回可读的Map
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	public  Map<String, Object> getParameterMap(HttpServletRequest request) {
	    // 参数Map
	    Map properties = request.getParameterMap();
	    // 返回值Map
	    Map<String,Object> returnMap = new HashMap<String,Object> ();
	    Iterator entries = properties.entrySet().iterator();
	    Map.Entry entry;
	    String name = "";
	    String value = "";
	    while (entries.hasNext()) {
	        entry = (Map.Entry) entries.next();
	        name = (String) entry.getKey();
	        Object valueObj = entry.getValue();
	        if(null == valueObj){
	            value = "";
	        }else if(valueObj instanceof String[]){
	            String[] values = (String[])valueObj;
	            for(int i=0;i<values.length;i++){
	                value = values[i] + ",";
	            }
	            value = value.substring(0, value.length()-1);
	        }else{
	            value = valueObj.toString();
	        }
	        returnMap.put(name, value);
	    }
	    return returnMap;
	}
}
